<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use think\Db;

/**
 * Assets逻辑
 */
class Assets extends LogicBase
{
    

    
    /**
     * 获取Assets列表
     */
    public function getAccountList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {
        



        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['game_id'])) {
            $where['a.game_id'] = $data['game_id'];
        }else{
            $where['a.game_id'] =['gt',0];
        }


        if (!empty($data['state'])) {
            $where['a.state'] = $data['state'];
        }

        $where['a.status'] =1;
        $where['a.type'] =4;
        if (!empty($where1)) {

            $list = $this->modelTransfer->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
        } else {

            $list = $this->modelTransfer->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
        }

        return $list;

    }


    /**
     * 获取Recharge列表
     */
    public function getRechargeList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {



        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['search_data'];
        }
        if (!empty($data['form_wallet_id'])) {
            $where['a.form_wallet_id'] = $data['form_wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }
        $where['a.status'] =1;
        $where['a.type'] =10;
        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        }
        $where['a.form_wallet_id']=6;
        $eth=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['a.form_wallet_id']=7;
        $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['a.form_wallet_id']=8;
        $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        return ['list'=>$list,'count'=>$count,'ect'=>$eth+0,'btc'=>$btc+0,'usdt'=>$usdt+0];

    }

    /**
     * 导出提币记录列表
     */
    public function exportRechargeList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {


        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['form_wallet_id'])) {
            $where['a.form_wallet_id'] = $data['form_wallet_id'];
        }

        if ( isset($data['state'])) {
            if (  !empty($data['state']) || $data['state']=='0') {
                $where['a.state'] = $data['state'];
            }
        }
        $where['a.status'] =1;
        $where['a.type'] =10;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            $list[$k]['wallet_name']=$v->wallet->data['name'];

            $list[$k]['state_text']=$v->state_text;
            if($v['state']==0){
                $list[$k]['up_time']='------';
            }else{
                $list[$k]['up_time']=date('Y-m-d H:i:s',$v->update_time);
            }
        }

        $titles = "序号,用户名,用户ID,币种,交易哈希,充值金额,状态,充值时间";
        $keys = "k,username,user_id,wallet_name,hash,number,state_text,create_time";

        action_log('导出', '导出充币记录列表');

        export_excel($titles, $keys, $list, '充币记录列表');

//      dd(json_decode(json_encode($list),true));

    }

  


    /**
     * 导出平台提现申请
     */
    public function exportTransferList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);

        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] = ['like', '%' . $data['search_data'] . '%'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }
        $where['a.status'] = 1;
        $where['a.state'] = 0;
        $where['a.type'] = 9;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c', 'a.user_id = c.id')->field($field)->where($where)->where(function ($q) use ($where1) {
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c', 'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k => $v) {
            $list[$k]['k'] =$k+1;
            $list[$k]['wallet_name'] = $v->towallet['name'];
            $list[$k]['get_money'] = $v->number - $v->poundage_number;
            $list[$k]['add_time'] = date('Y-m-d H:i:s', $v->create_time);

        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,用户账号,提现金额,手续费,到账金额（闪兑）,提现币种,支付数量,钱包地址,申请时间";
        $keys = "k,id,username,number,poundage_number,get_money,wallet_name,coin_number,to,add_time";

        action_log('导出', '导出平台提现申请(未审核');

        export_excel($titles, $keys, $list, '平台提现申请(未审核)');

    }

        /**
         * 平台提现已处理列表
         */
        public function getCheckTransferList($data = [], $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {


        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1']) ) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] =$data['wallet_id'] ;
        }
        $where['a.status'] =1;
        $where['a.type'] =9;
        $where['a.state'] =['neq',0];
        if (!empty($where1)) {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);

            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->field($field)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->count();
        }

        $ke=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('number');
        $where['a.to_wallet_id'] =6;
        $eth=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('coin_number');
        $where['a.to_wallet_id'] =7;
        $btc=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('coin_number');
        $where['a.to_wallet_id'] =8;
        $usdt=$this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->where($where)->sum('coin_number');
        $ke=$ke?$ke:0;
        $eth=$eth?$eth:0;
        $btc=$btc?$btc:0;
        $usdt=$usdt?$usdt:0;
        return ['count'=>$count,'list'=>$list,'ke'=>$ke,'eth'=>$eth,'btc'=>$btc,'usdt'=>$usdt];

    }

/**
 * 充值重新采集
 */
public function recollect($data=[]){
    $id= $data['id'];
    $res = Db::name("recharge_auto")->where(["id"=>$id])->update(["status"=>0,"collect_count"=>0]);
    if($res){
        return [RESULT_SUCCESS, '成功'];
    }else{
        return [RESULT_ERROR, '失败'];
    }
}
    /**
     * 导出平台提现申请（已处理）
     */
    public function exportCheckTransferList($data, $field = 'a.*,c.username', $order = 'a.id asc')
    {

        set_time_limit(0);

        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['state'])) {
            $where['a.state'] = $data['state'];
        }else{
            $where['a.state'] = ['neq',0];
        }
        $where['a.status'] = 1;

        $where['a.type'] = 9;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c', 'a.user_id = c.id')->field($field)->where($where)->where(function ($q) use ($where1) {
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c', 'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k => $v) {
$list[$k]['k'] =$k+1;
            $list[$k]['wallet_name'] = $v->towallet['name'];
            $list[$k]['get_money'] = $v->number - $v->poundage_number;
            $list[$k]['state_text'] = $v->state_text;
            $list[$k]['add_time'] = date('Y-m-d H:i:s', $v->create_time);
            $list[$k]['update_time']=$v['update_time']?date('Y-m-d H:i:s',$v['update_time']):'';
        }

//        dd(json_decode(json_encode($list),true));
        $titles = "序号,ID,用户账号,提现金额,手续费,到账金额（闪兑）,提现币种,支付数量,钱包地址,申请时间,状态,审核时间,审核人";
        $keys = "k,id,username,number,poundage_number,get_money,wallet_name,coin_number,to,add_time,state_text,update_time,admin_name,";

        action_log('导出', '导出平台提现申请(已处理');

        export_excel($titles, $keys, $list, '平台提现申请(已处理)');

    }









}
